home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
050
/
tptool.lbr
/
CHAPTER1.PQS
/
chapter1.pas
Wrap
Pascal/Delphi Source File
|
1985-06-03
|
3KB
|
144 lines
{$A-}
program chapter1;
{$I TOOLU.PAS}
{ Note: X$ disables the include file }
{X$I OS-CPM80.PAS <-- CP/M-80 users include this file }
{X$I OS-CPM86.PAS <-- CP/M-86 users include this file }
{X$I OS-MSDOS.PAS <-- MS-DOS v 2 users include this file }
{$I OS-OTHER.PAS <-- MS-DOS v 1 and all others }
{ OS support is not in chapter1 of K&P, but this is a good place to add it }
PROCEDURE COPY;
VAR C:CHARACTER;
BEGIN
WHILE(GETC(C)<>ENDFILE)DO
PUTC(C)
END;
PROCEDURE CHARCOUNT;
VAR
NC:INTEGER;
C:CHARACTER;
BEGIN
NC:=0;
WHILE (GETC(C)<>ENDFILE)DO
NC:=NC+1;
PUTDEC(NC,1);
PUTC(NEWLINE)
END;
PROCEDURE LINECOUNT;
VAR
N1:INTEGER;
C:CHARACTER;
BEGIN
N1:=0;
WHILE(GETC(C)<>ENDFILE)DO
IF(C=NEWLINE)THEN
N1:=N1+1;
PUTDEC(N1,1);
PUTC(NEWLINE)
END;
PROCEDURE CallShell;
{ read first line of STDIN, put in process queue }
{ W. Kempton -- 5 Jan 85 }
begin
if ActiveProcessQ then
ERROR('Shell: Processes already queued -- aborted');
ActiveProcessQ := GETLINE(ProcessQueue,STDIN,MAXSTR);
end;
PROCEDURE WORDCOUNT;
VAR
NW:INTEGER;
C:CHARACTER;
INWORD:BOOLEAN;
BEGIN
NW:=0;
INWORD:=FALSE;
WHILE(GETC(C)<>ENDFILE)DO
IF(C=BLANK)OR(C=NEWLINE)OR(C=TAB) THEN
INWORD:=FALSE
ELSE IF (NOT INWORD)THEN BEGIN
INWORD:=TRUE;
NW:=NW+1
END;
PUTDEC(NW,1);
PUTC(NEWLINE)
END;
PROCEDURE DETAB;
CONST
MAXLINE=1000;
TYPE
TABTYPE=ARRAY[1..MAXLINE] OF BOOLEAN;
VAR
C:CHARACTER;
COL:INTEGER;
TABSTOPS:TABTYPE;
FUNCTION TABPOS(COL:INTEGER;VAR TABSTOPS:TABTYPE)
:BOOLEAN;
BEGIN
IF(COL>MAXLINE)THEN
TABPOS:=TRUE
ELSE
TABPOS:=TABSTOPS[COL]
END;
PROCEDURE SETTABS(VAR TABSTOPS:TABTYPE);
CONST
TABSPACE= TabSpaces ; { was 4 in K&P }
VAR
I:INTEGER;
BEGIN
FOR I:=1 TO MAXLINE DO
TABSTOPS[I]:=(I MOD TABSPACE = 1)
END;
BEGIN
SETTABS(TABSTOPS);
COL:=1;
WHILE(GETC(C)<>ENDFILE)DO
IF(C=TAB)THEN
REPEAT
PUTC(BLANK);
COL:=COL+1
UNTIL(TABPOS(COL,TABSTOPS))
ELSE IF(C=NEWLINE)THEN BEGIN
PUTC(NEWLINE);
COL:=1
END
ELSE BEGIN
PUTC(C);
COL:=COL+1
END
END;
PROCEDURE COMMAND;
BEGIN
IF (GlobalArg1='copy') THEN COPY
ELSE IF (GlobalArg1='charcount') THEN CHARCOUNT
ELSE IF (GlobalArg1='linecount') THEN LINECOUNT
ELSE IF (GlobalArg1='wordcount') THEN WORDCOUNT
ELSE IF (GlobalArg1='detab') THEN DETAB
ELSE IF (GlobalArg1='list') THEN listcat
ELSE IF (GlobalArg1='shell') THEN CallShell
ELSE error('Chap 1: can''t happen');
END;(*COMMAND*)
BEGIN
command;
ENDCMD;
END.